package cn.com.controler.Article;
import cn.com.controler.Util.JDBCUtil;
import cn.com.entity.Article;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ArticleDAOTemplete {

    //添加数据
    public static int insertdata(String str,Object[]arr)
    {
        Connection coll = null;
        PreparedStatement statement = null;
        try {
            coll = JDBCUtil.getConnection();
            statement = coll.prepareStatement(str);
            //设置参数
            for (int i = 0; i < arr.length ; i++) {
                statement.setObject(i+1,arr[i]);
            }
            //执行
            return statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(coll,statement,null);
        }
        return 0;
    }


    //查询数据
    public static List selectdata(String str, Object[]arr)
    {
        Connection coll = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List list = new ArrayList();
        Date fbdate = new Date();
        try {
            coll = JDBCUtil.getConnection();
            statement = coll.prepareStatement(str);
            //设置参数
            for (int i = 0; i < arr.length ; i++) {
                statement.setObject(i+1,arr[i]);
            }
            //执行
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                fbdate = resultSet.getTimestamp("ArticlePublishTime");
                SimpleDateFormat sa = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String format = sa.format(fbdate);
                Article article = new Article();
                article.setArticleId(resultSet.getInt("ArticleId"));
                article.setArticleUserId(resultSet.getInt("ArticleUserId"));
                article.setArticleAdminId(0);
                article.setArticleTitle(resultSet.getString("ArticleTitle"));
                article.setArticleContent(resultSet.getString("ArticleContent"));
                article.setArticlePublishTime(format);
                article.setArticleType(resultSet.getInt("ArticleType"));
                list.add(article);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(coll,statement,resultSet);
        }
        return null;
    }
}
